package cn.com.wxd.service.robot.tuling.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.com.wxd.service.robot.tuling.entity.ChilSongTall;
import cn.com.wxd.service.robot.tuling.entity.MenuTall;
import cn.com.wxd.service.robot.tuling.entity.NewsTall;
import cn.com.wxd.service.robot.tuling.entity.TextTall;
import cn.com.wxd.service.robot.tuling.RobotTall;
import cn.com.wxd.service.robot.tuling.entity.LinkTall;
import cn.com.wxd.service.robot.tuling.entity.PoemTall;
import cn.com.wxd.service.robot.tuling.entity.TallMenuList;
import cn.com.wxd.service.robot.tuling.entity.TallNewsList;
import cn.com.wxd.common.JsonUtil;

/**
 * Title:返回消息类型类
 * Description:cn.com.wxd.service.robot.tuling.util
 * 
 * Company: wxd网站系统
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-8-25 下午5:47:12
 *
 * @author WangXuDong
 * @version 1.0
 */
public class RetTallTypeCode {
	/**
	 * 内部对象用于降服务器返回的信息转成对象
	 * @param sb 服务器返回的json
	 * @return RobotTall机器人回复信息对象
	 * @throws Exception 
	 */
	public static RobotTall ConvertStringToRobotall(String sb) throws Exception{
		Map<String,String> map = JsonUtil.jsonToStrMap(sb);
		if(map==null||map.get("code")==null||map.get("code").trim().equals("")){
			return null;
		}
		String code = map.get("code");
		RobotTall rot = null;
		if(code.equals(RobotTallType.TEXT_TALL.getValue())){  //文本
			TextTall tt = new TextTall();
			tt.setCode(code);
			tt.setText(map.get("text"));
			rot = tt;
		}
		if(code.equals(RobotTallType.LINK_TALL.getValue())){  //连接
			LinkTall lt = new LinkTall();
			lt.setCode(code);
			lt.setText(map.get("text"));
			lt.setUrl(map.get("url"));
			rot = lt;
		}
		if(code.equals(RobotTallType.NEWS_TALL.getValue())){ //新闻
			NewsTall nt = new NewsTall();
			nt.setCode(code);
			nt.setText(map.get("text"));
			String jsonarr = map.get("list");
			List<Map<String,String>> listmap = JsonUtil.jsonToListOfStrMap(jsonarr);
			List<TallNewsList> list = new ArrayList<TallNewsList>();
			for (int i = 0; i < listmap.size(); i++) {
				TallNewsList td = new TallNewsList();
				Map<String,String> map22 = listmap.get(i);
				td.setArticle(map22.get("article"));
				td.setSource(map22.get("source"));
				td.setIcon(map22.get("icon"));
				td.setDetailUrl(map22.get("detailurl"));
				list.add(td);
			}
			nt.setNewsList(list);
			rot = nt;
		}
		if(code.equals(RobotTallType.MENU_TALL.getValue())){  //菜谱
			MenuTall mt = new MenuTall();
			mt.setCode(code);
			mt.setText(map.get("text"));
			String jsonarr = map.get("list");
			List<Map<String,String>> listmap = JsonUtil.jsonToListOfStrMap(jsonarr);
			List<TallMenuList> list = new ArrayList<TallMenuList>();
			for (int i = 0; i < listmap.size(); i++) {
				TallMenuList td = new TallMenuList();
				Map<String,String> map22 = listmap.get(i);
				td.setName(map22.get("name"));
				td.setInfo(map22.get("info"));
				td.setIcon(map22.get("icon"));
				td.setDetailurl(map22.get("detailurl"));
				list.add(td);
			}
			mt.setMenuList(list);
			rot = mt;
		}
		if(code.equals(RobotTallType.CHILSONG_TALL.getValue())){   //儿歌（儿童版）
			ChilSongTall ct = new ChilSongTall();
			ct.setCode(code);
			ct.setText(map.get("text"));
			String functionjson = map.get("function");
			Map<String,String> map222 = JsonUtil.jsonToStrMap(functionjson);
			ct.setFunction(map222.get("song"),map222.get("singer"));
			rot = ct;
		}
		if(code.equals(RobotTallType.POEM_TALL.getValue())){   //诗歌（儿童版）
			PoemTall pt = new PoemTall();
			pt.setCode(code);
			pt.setText(map.get("text"));
			String functionjson = map.get("function");
			Map<String,String> map222 = JsonUtil.jsonToStrMap(functionjson);
			pt.setFunction(map222.get("author"),map222.get("name"));
			rot = pt;
		}
		return rot;
	}
}
